window.addEventListener('message', function(event){
    let data = event.data.data;
    let file = event.data.file;
    preuploadFile(data, function(response) {
        if (response.flag) {
            var result = {};
            result.code = 2;
            result.data = response.data;
            top.postMessage(result, "*");
        } else {
            let formData = new FormData();
            formData.append("file", file);
            formData.append("ciphertext", data.ciphertext);
            uploadFile(data.userId, formData);
        }
    });
}, false);

function preuploadFile(data, successFunc) {
    let url = '/encrypts/files/preload/'+data.userId;
    $.ajax({
            type: 'POST',
            url: url,
            data: data.ciphertext,
            success: function(data, textStatus, jqXHR) {
                successFunc(data);
            },
            error: function(jqXHR) {
                console.error(jqXHR);
            }
    });
};
        
function uploadFile(userId, formData) {
    let url = '/encrypts/files/upload/'+userId;
    $.ajax({
        type: "POST",
        url: url,
        data: formData,
        processData: false,
        contentType: false,
        xhr: function () {
         var xhr = $.ajaxSettings.xhr();
            if (xhr.upload) {
                //处理进度条的事件
                xhr.upload.addEventListener("progress", function(e) {
                    var curr=e.loaded;
                    var total=e.total;
                    var process = Math.floor((curr / total) * 100);
                    
                    var result = {};
                    result.code = 0;
                    result.data = process
                    top.postMessage(result, "*");
                 }, false);
            }
            
            //加载完成的事件
            xhr.addEventListener("load", function(e) {
                var result = {};
                result.code = 1;
                top.postMessage(result, "*");
             }, false);
            
            //加载出错的事件
            xhr.addEventListener("error", function(e) {
            var result = {};
                result.code = -1;
                top.postMessage(result, "*");
             }, false);
            
            //加载取消的事件
            xhr.addEventListener("abort", function(e) {
            var result = {};
                result.code = -2;
                top.postMessage(result, "*");
             }, false);
            
            return xhr;
        },
        success: function (data) {
            var result = {};
            result.code = 2;
            result.data = data;
            top.postMessage(result, "*");
        },
        failure: function (data) {
            console.info(data);
            var result = {};
            result.code = -1;
            result.data = jqXHR.responseText;
            top.postMessage(result, "*");
        },
        error: function(jqXHR) {
            console.info(jqXHR);
            var result = {};
            result.code = -1;
            result.data = jqXHR.responseText;
            top.postMessage(result, "*");
        }
    });
};